//
//  SettingViewController.m
//  PeopleStreet
//
//  Created by 辰领科技 on 16/3/8.
//  Copyright © 2016年 yuf. All rights reserved.
//

#import "SettingViewController.h"
//二维码
#import "LBXScanView.h"
#import <objc/message.h>
#import "LBXScanResult.h"
#import "LBXScanWrapper.h"
#import "SubLBXScanViewController.h"
#import "MyQRViewController.h"
#import "ScanResultViewController.h"
#import "NormalPrombleViewController.h"
#import "WebDetailInfoViewController.h"
#import "LoginViewController.h"
@interface SettingViewController ()<UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate>
@property (strong, nonatomic) IBOutlet UIView *navView;
@property (strong, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation SettingViewController

- (void)viewWillAppear:(BOOL)animated{
    [self.tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initViewLayout];
    // Do any additional setup after loading the view.
}

- (void)initViewLayout{
    [self adaptiveViewLayout:self.view];
    [self adaptiveViewLayout:self.navView];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;

}

- (IBAction)back:(id)sender {
    [self.navigationController  popViewControllerAnimated:YES];
}


#pragma mark --模仿支付宝
- (void)ZhiFuBaoStyle
{
    //设置扫码区域参数
    LBXScanViewStyle *style = [[LBXScanViewStyle alloc]init];
    style.centerUpOffset = 60;
    style.xScanRetangleOffset = 30;
    
    if ([UIScreen mainScreen].bounds.size.height <= 480 )
    {
        //3.5inch 显示的扫码缩小
        style.centerUpOffset = 40;
        style.xScanRetangleOffset = 20;
    }
    
    
    style.alpa_notRecoginitonArea = 0.6;
    
    style.photoframeAngleStyle = LBXScanViewPhotoframeAngleStyle_Inner;
    style.photoframeLineW = 2.0;
    style.photoframeAngleW = 16;
    style.photoframeAngleH = 16;
    
    style.isNeedShowRetangle = NO;
    
    style.anmiationStyle = LBXScanViewAnimationStyle_NetGrid;
    
    //使用的支付宝里面网格图片
    UIImage *imgFullNet = [UIImage imageNamed:@"CodeScan.bundle/qrcode_scan_full_net"];
    
    
    style.animationImage = imgFullNet;
    
    
    [self openScanVCWithStyle:style];
}

- (void)openScanVCWithStyle:(LBXScanViewStyle*)style
{
    UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"PersonCenterStoryboard" bundle:nil];
    SubLBXScanViewController *vc = [secondStroyBoard instantiateViewControllerWithIdentifier:@"SubLBXScanView"];
    vc.style = style;
    [self.navigationController pushViewController:vc animated:YES];
}



#pragma mark ===== UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex==1) {
        [[SDImageCache sharedImageCache] clearDisk];//清理磁盘
        [[SDImageCache sharedImageCache] clearMemory];//清理内存
        [[CWHUDTips sharedInstance]  showTips:@"清理缓存成功"  ];
    }
}

#pragma mark -- UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentify = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify ];
    if (cell==nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentify];
    }
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    for (UIView *view in cell.subviews) {
        if ([view isKindOfClass:[UILabel class]] ||[view isKindOfClass:[UIButton class] ]||[view isKindOfClass:[UIView class]]) {
            [view removeFromSuperview];
        }
    }
    NSArray * array = @[@"清除缓存",@"使用帮助",@"常见问题",@"平台简介",@"扫一扫",@"修改密码",@"收货地址管理",@"退出登录"];
    UILabel * label = [[UILabel alloc]init];
    label.font = [UIFont systemFontOfSize:15*VerticalTexeRatio()];
    label.text = array[indexPath.row];
    [cell addSubview:label];
    
    UIImageView  * imageView = [[UIImageView alloc]initWithFrame:flexibleFrame(CGRectMake(300, 17, 8, 13), YES)];
    imageView.image = [UIImage imageNamed:@"jiantou"];
    
    if (indexPath.row!=7) {
        label.frame = flexibleFrame(CGRectMake(14, 0, 200, 47), NO);
        label.textAlignment = NSTextAlignmentLeft;
        label.textColor = [UIColor colorWithWhite:0.220 alpha:1.000];
        
        UIView * line = [[UIView alloc]initWithFrame:flexibleFrame(CGRectMake(0, 46, 320, 1), NO)];
        line.backgroundColor = [UIColor colorWithWhite:0.886 alpha:1.000];
        [cell addSubview:imageView];
        if ([Common getLoginStatus]) {
            if(indexPath.row != 6){
                [cell addSubview:line];
            }
        }else{
            [cell addSubview:line];
        }
        
    }else{
        imageView.frame = flexibleFrame(CGRectMake(300, 23, 8, 13), YES);
        label.frame = flexibleFrame(CGRectMake(0, 0, 320, 55), NO);
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor colorWithRed:1.000 green:0.427 blue:0.337 alpha:1.000];
        UIView * line = [[UIView alloc]initWithFrame:flexibleFrame(CGRectMake(0, 0, 320, 6), NO)];
        line.backgroundColor = [UIColor colorWithWhite:0.957 alpha:1.000];
        [cell addSubview:line];
        
        UIView * Line = [[UIView alloc]initWithFrame:flexibleFrame(CGRectMake(0, 49, 320, 6), NO)];
        Line.backgroundColor = [UIColor colorWithWhite:0.957 alpha:1.000];
        [cell addSubview:Line];
        

    }
    
   
    
    
   
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([Common getLoginStatus]) {
        return 8;
    }else{
        return 7;
    }
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row!=7) {
        return 47*VerticalRatio();

    }else{
    return 55*VerticalRatio();
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row == 0){
        CGFloat size = [[SDImageCache sharedImageCache]getSize];
        NSString * sizeStr = [NSString stringWithFormat:@"%.1fM",size /1000/1000];
        UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:[NSString stringWithFormat:@"共%@图片缓存,确定清除？",sizeStr] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alertView show];
    }else if (indexPath.row == 1) {
        UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"PersonCenterStoryboard" bundle:nil];
        NormalPrombleViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"NormalPromble"];
        page.typeStr = @"2";//使用帮助
        [self.navigationController  pushViewController:page animated:YES];
        
    }else if (indexPath.row == 2) {
        UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"PersonCenterStoryboard" bundle:nil];
        NormalPrombleViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"NormalPromble"];
        page.typeStr = @"1";//常见问题
        [self.navigationController  pushViewController:page animated:YES];
   
    }else if (indexPath.row == 3) {
        UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"PersonCenterStoryboard" bundle:nil];
        WebDetailInfoViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"WebDetailInfo"];
        page.objectId = @"3";//平台简介
        page.titleStr = @"平台简介";
        [self.navigationController  pushViewController:page animated:YES];
        
    }else if (indexPath.row == 4) {
//        if (![Common getLoginStatus]) {
//            [self ShowLoginPage];
//        }else{
            [self ZhiFuBaoStyle];
//        }
    }else if (indexPath.row == 5) {
        UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"PersonCenterStoryboard" bundle:nil];
        UIViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"ChangePassword"];
        [self.navigationController  pushViewController:page animated:YES];
        
    }else if (indexPath.row==6){
        UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"PersonCenterStoryboard" bundle:nil];
        UIViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"ControlAddress"];
        [self.navigationController  pushViewController:page animated:YES];
        
    }else if (indexPath.row==7){
       
        if ([Common getLoginStatus]) {
            NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
            
            self.shareApi.delegate=self;
            [self.shareApi  requestOperationWithDic:dic withPath:SX_exitLogin withHttpType:@"POST" withTag:SX_SX_exitLogin];
        }else{
            UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"HomeStoryboard" bundle:nil];
            LoginViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"Login"];
            page.pageFinishBlock  = ^(){
                [self.navigationController  popViewControllerAnimated:YES];
            };
            [self presentViewController:page animated:YES completion:^{}];
        }
    }
    
    
}


#pragma   mark---- GetADBanner Request Delegate

-(void)fetchDatabaseFinished:(NSMutableDictionary *)database withTag:(NSInteger)tag{
    if (tag==SX_SX_exitLogin) {
        
        if (  [[database valueForKey:@"type"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"HomeStoryboard" bundle:nil];
            LoginViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"Login"];
            page.pageFinishBlock  = ^(){
                [self.navigationController  popViewControllerAnimated:YES];
            };
            MyNavigationViewController * navC = (MyNavigationViewController *)self.navigationController;
            [navC pushToViewController:page ];
            [Common DeleteSingleFile];
        }
    }
}
-(void)fetchDatabaseFailed:(NSError *)error message:(NSString *)message{
        
        //
        [self dismissWaitDialog];
        
        
    }

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
